package com.yoyo.generator.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.yoyo.common.constant.GenConstants;
import com.yoyo.common.core.text.Convert;
import com.yoyo.common.utils.StringUtils;
import com.yoyo.generator.domain.GenTable;
import com.yoyo.generator.domain.GenTableColumn;
import com.yoyo.generator.mapper.GenTableColumnMapper;
import com.yoyo.generator.mapper.GenTableMapper;
import com.yoyo.generator.service.IGenTableService;
import com.yoyo.generator.util.GenUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author zyl
 * @Date 2021/6/12 -15:55
 */
@Service
@Slf4j
public class GenTableServiceImpl implements IGenTableService {

    @Autowired
    private GenTableMapper genTableMapper;
    @Autowired
    private GenTableColumnMapper genTableColumnMapper;

    @Override
    public void importGenTable(List<GenTable> tableList, String loginName) {
        try {
            tableList = tableList.stream()
                    .peek(table -> GenUtils.initTable(table, loginName))
                    .collect(Collectors.toList());

            genTableMapper.batchInsertGenTable(tableList);

            List<String> tableNames = tableList.stream()
                    .map(GenTable::getTableName)
                    .collect(Collectors.toList());

            List<GenTableColumn> genTableColumns = genTableColumnMapper.selectGenTableColumnByNames(tableNames);
            Map<String, GenTable> mapTable = tableList.stream()
                    .collect(Collectors.toMap(GenTable::getTableName, Function.identity()));

            for (GenTableColumn genTableColumn : genTableColumns) {
                GenTable table = mapTable.get(genTableColumn.getTableName());
                GenUtils.initColumnField(genTableColumn, table);
            }
            genTableColumnMapper.batchInsertTableColumn(genTableColumns);

        } catch (Exception e) {
            log.error("导入异常", e);
        }
    }

    @Override
    public void deleteGenTableByIds(String ids) {
        genTableColumnMapper.deleteGenTableColumn(Convert.toLongArray(ids));
        genTableMapper.deleteGenTableByIds(Convert.toLongArray(ids));
    }

    @Override
    public List<GenTable> selectDbTableListByNames(String[] tableNames) {

        return genTableMapper.selectDbTableListByNames(tableNames);
    }

    @Override
    public List<GenTable> selectDbTableList(GenTable genTable) {

        return genTableMapper.selectDbTableList(genTable);
    }

    @Override
    public GenTable selectGenTableById(Long tableId) {
        GenTable table = genTableMapper.selectGenTableById(tableId);
        setTableFromOptions(table);

        return table;
    }

    private void setTableFromOptions(GenTable table) {
        JSONObject paramsObj = JSONObject.parseObject(table.getOptions());
        if (StringUtils.isNotNull(paramsObj)) {
            String treeCode = paramsObj.getString(GenConstants.TREE_CODE);
            String treeParentCode = paramsObj.getString(GenConstants.TREE_PARENT_CODE);
            String treeName = paramsObj.getString(GenConstants.TREE_NAME);
            String parentMenuId = paramsObj.getString(GenConstants.PARENT_MENU_ID);
            String parentMenuName = paramsObj.getString(GenConstants.PARENT_MENU_NAME);

            table.setTreeCode(treeCode);
            table.setTreeParentCode(treeParentCode);
            table.setTreeName(treeName);
            table.setParentMenuId(parentMenuId);
            table.setParentMenuName(parentMenuName);

        }

    }

    @Override
    public List<GenTable> selectGenTableList(GenTable genTable) {

        return genTableMapper.selectGenTableList(genTable);
    }

    @Override
    public List<GenTable> selectGenTableAll() {

        return genTableMapper.selectGenTableAll();
    }
}
